package view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.concerneyes.R;

import common.AbsBaseView;


/**
 * 
 * Depiction: 主界面底部导航栏
 * <p>
 * Modify:
 * <p>
 * Author: Kevin Lynn
 * <p>
 * Create Date：2016年11月11日 上午10:10:32
 * <p>
 * 
 * @version 1.0
 * @since 1.0
 */
public class BottomTabBar extends AbsBaseView implements View.OnClickListener{
    private int curTab = -1;
    private RelativeLayout[] tabs = new RelativeLayout[5];
    private ImageView[] tbIvs = new ImageView[tabs.length];
    private TextView[] tbTvs = new TextView[tabs.length];

    private TextView countTv;

    private final int[] tabIconSelected = new int[]{
            R.drawable.tab_dongtai_selected,
            R.drawable.tab_dongtai_selected,
            R.drawable.tab_dongtai_selected,
            R.drawable.tab_dongtai_selected,
            R.drawable.tab_dongtai_selected
    };

    private final int[] tabIconUnselected = new int[]{
            R.drawable.tab_dongtai,
            R.drawable.tab_dongtai,
            R.drawable.tab_dongtai,
            R.drawable.tab_dongtai,
            R.drawable.tab_dongtai
    };

    public BottomTabBar(Context context) {
        super(context);
        init(context);
    }

    public BottomTabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public BottomTabBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        countTv = (TextView) findViewById(R.id.countTv);
        tabs[0] = (RelativeLayout) findViewById(R.id.tab_biz);
        tabs[1] = (RelativeLayout) findViewById(R.id.tab_msg);
        tabs[2] = (RelativeLayout) findViewById(R.id.tab_affair);
        tabs[3] = (RelativeLayout) findViewById(R.id.tab_contact);
        tabs[4] = (RelativeLayout) findViewById(R.id.tab_me);

        tbIvs[0] = (ImageView) findViewById(R.id.tab_iv_biz);
        tbIvs[1] = (ImageView) findViewById(R.id.tab_iv_msg);
        tbIvs[2] = (ImageView) findViewById(R.id.tab_iv_affair);
        tbIvs[3] = (ImageView) findViewById(R.id.tab_iv_contact);
        tbIvs[4] = (ImageView) findViewById(R.id.tab_iv_me);

        tbTvs[0] = (TextView) findViewById(R.id.tab_tv_biz);
        tbTvs[1] = (TextView) findViewById(R.id.tab_tv_msg);
        tbTvs[2] = (TextView) findViewById(R.id.tab_tv_affair);
        tbTvs[3] = (TextView) findViewById(R.id.tab_tv_contact);
        tbTvs[4] = (TextView) findViewById(R.id.tab_tv_me);

        for (int i = 0; i < tabs.length; i++) {
            RelativeLayout item = tabs[i];
            item.setTag(String.valueOf(i));
            item.setOnClickListener(this);
        }
    }

    @Override
    public int layoutResId() {
        return R.layout.tab_bar_bottom;
    }

    public void setCurTab(int index){
        onClick(tabs[index]);
    }

    public int getTabIndex(){
        return curTab;
    }

    public String getTabText(int index){
        return tbTvs[index].getText().toString();
    }

    public void showCount(int num){
        if (num > 0) {
            countTv.setText(String.valueOf(num));
            countTv.setVisibility(View.VISIBLE);
        } else {
            countTv.setText("");
            countTv.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onClick(View view) {
        int actionIndex = Integer.parseInt(view.getTag().toString());

        if (actionIndex == curTab) {
            return;
        }

        curTab = actionIndex;

        for (int i = 0; i < tabs.length; i++) {
            RelativeLayout item = tabs[i];
            if (item.getId() == view.getId()) {
                tbIvs[i].setImageResource(tabIconSelected[i]);
                tbTvs[i].setTextColor(getResources().getColor(R.color.blue));
            } else {
                tbIvs[i].setImageResource(tabIconUnselected[i]);
                tbTvs[i].setTextColor(getResources().getColor(R.color.tabTextColor));
            }
        }

        if (tabBarChangeListener != null) {
            tabBarChangeListener.onTabBarChange(curTab,view.getId());
        }
    }

    public interface TabBarChangeListener {
        void onTabBarChange(int position, int tabId);
    }

    private TabBarChangeListener tabBarChangeListener;

    public TabBarChangeListener getTabBarChangeListener() {
        return tabBarChangeListener;
    }

    public void setTabBarChangeListener(TabBarChangeListener tabBarChangeListener) {
        this.tabBarChangeListener = tabBarChangeListener;
    }

    @Override
    public void loadData(Object data) {

    }

    @Override
    public boolean isEmpty() {
        return false;
    }

}
